package cn.net.chenbao.medical.mvp;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

import cn.net.chenbao.medical.MedicalApplication;
import cn.net.chenbao.medical.api.Api;
import cn.net.chenbao.medical.bean.Ks;
import cn.net.chenbao.medical.bean.Patient;
import cn.net.chenbao.medical.bean.Pyj;
import cn.net.chenbao.medical.bean.Report;
import cn.net.chenbao.medical.mvp.presenter.IAbnormalP;
import cn.net.chenbao.medical.mvp.view.IAbnormalV;
import cn.net.chenbao.medical.util.Consts;
import cn.net.chenbao.medical.xutils.WWXCallBack;

/**
 * Created by 木头 on 2018/1/25.
 */

public class AbnormalP implements IAbnormalP {
    RequestParams requestParams;

    private IAbnormalV mAbnormalV;

    public AbnormalP(IAbnormalV abnormalV) {
        this.mAbnormalV = abnormalV;
    }

    @Override
    public void PyjList() {
        mAbnormalV.loadStart(true);
        RequestParams requestParams = new RequestParams(Api.PyjList());
        requestParams.addBodyParameter("sessionId", MedicalApplication.getInstance()
                .getSessionId());
        x.http().get(requestParams, new WWXCallBack("PyjList") {
            @Override
            public void onAfterSuccessOk(JSONObject data) {
                List<Pyj> patients = JSONArray.parseArray(data.getString("Data"), Pyj.class);
                mAbnormalV.InitPyjListSuccess(patients);
            }

            @Override
            public void onAfterFinished() {
                mAbnormalV.loadFinish();
            }
        });
    }

    @Override
    public void KsList() {
        mAbnormalV.loadStart(true);
        RequestParams requestParams = new RequestParams(Api.KsList());
        requestParams.addBodyParameter("sessionId", MedicalApplication.getInstance()
                .getSessionId());


        x.http().get(requestParams, new WWXCallBack("KsList") {
            @Override
            public void onAfterSuccessOk(JSONObject data) {
                List<Ks> ks = JSONArray.parseArray(data.getString("Data"), Ks.class);
                mAbnormalV.InitKsListSuccess(ks);
            }

            @Override
            public void onAfterFinished() {
                mAbnormalV.loadFinish();
            }
        });

    }

    @Override
    public void doAbnormal(int mode, String startDate, int pyFlag, long printId, String pyj, String ksbm) {
        mAbnormalV.loadStart(true);
        String result = null;
        switch (mode) {
            case Consts.MODE_NURSE:
                //配液报表
                result = "PyAbnormal";
                requestParams = new RequestParams(Api.PyAbnormal());
                requestParams.addBodyParameter("rptDate", startDate);
                requestParams.addBodyParameter("printId", printId + "");
                if (!TextUtils.isEmpty(pyj))
                    requestParams.addBodyParameter("pyjNo", pyj);
                break;
            case Consts.MODE_JC:
                //进仓报表
                result = "InAbnormal";
                requestParams = new RequestParams(Api.InAbnormal());
                requestParams.addBodyParameter("rptDate", startDate);
                requestParams.addBodyParameter("printId", printId + "");
                if (!TextUtils.isEmpty(pyj))
                    requestParams.addBodyParameter("pyj", pyj);
                break;
            case Consts.MODE_KBCC:
                //空仓出仓报表
                result = "OutAbnormal";
                requestParams = new RequestParams(Api.OutAbnormal());
                requestParams.addBodyParameter("rptDate", startDate);
                requestParams.addBodyParameter("printId", printId + "");

                /*****************************************************************/
                //空仓报表参数
                requestParams.addBodyParameter("pyFlag", 2 + "");
                if (!TextUtils.isEmpty(ksbm)) {
                    requestParams.addBodyParameter("ksbm", ksbm);
                }
                break;
            case Consts.MODE_APOTHECARY:
                //出仓报表
                result = "OutAbnormal";
                requestParams = new RequestParams(Api.OutAbnormal());
                requestParams.addBodyParameter("rptDate", startDate);
                requestParams.addBodyParameter("printId", printId + "");
                break;
            case Consts.MODE_HSJJ:
                //交接报表
                result = "JiaoAbnormal";
                requestParams = new RequestParams(Api.JiaoAbnormal());
                requestParams.addBodyParameter("rptDate", startDate);
                requestParams.addBodyParameter("pyFlag", pyFlag + "");
                break;
        }
        requestParams.addBodyParameter("sessionId", MedicalApplication.getInstance()
                .getSessionId());
        x.http().get(requestParams, new WWXCallBack(result) {
            @Override
            public void onAfterSuccessOk(JSONObject data) {
                List<Patient> patients = JSONArray.parseArray(data.getString("Data"), Patient.class);
                if (patients != null) {
                    mAbnormalV.InitAbnormalSuccess(patients);
                }
            }

            @Override
            public void onAfterFinished() {
                mAbnormalV.loadFinish();
            }
        });
    }
}
